package com.youtellv1.utils;

import com.youtellv1.entity.Admin;
import com.youtellv1.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/home/")
    public String homePage() {
        return "admin";
    }

    @PostMapping("/admin/")
    public String login(Admin param, HttpSession session, Model model) {
        Admin admin = adminService.findByAccountAndPassword(param);
        if (admin != null) {
            session.setAttribute("username", admin.getAccount());
            return "redirect:/home/";
        }

        Admin adminByAccount = adminService.findByAccount(param);
        if (adminByAccount == null) {
            model.addAttribute("msg", "未查询到该用户");
        } else {
            model.addAttribute("msg", "请检查密码");
        }
        return "index";
    }

    @GetMapping("/exit/")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }

}